<?php
class ArticleModel extends Model {
	public function __construct() {
		parent::__construct('Article');
	}
	
	public function addOne(& $data) {
		return $this->data($data)->add();
	}
	
	public function getOneByPk($id) {
		return $this->where('id='.$id)->find();
	}
	
	public function updateOne($where, & $data) {
		return $this->where($where)->save($data);
	}
	
	public function deleteOne($where) {
		return $this->where($where)->delete();
	}
	
	public function getListByPage($pageSize = 20) {
		import('ORG.Util.Page');
		$pre = C('DB_PREFIX');
		
		$count = $this->count();	
		if(empty($count)) $count = 0;
		$page = new Page($count, $pageSize);
		$pageString = $page->show();
		
		$sql = "select l.id,l.title,l.author,l.isrecommend,l.posttime,l.viewnum,r.typename from {$pre}article l left join {$pre}articletype r on l.articletypeid=r.id where l.id<=(select id from {$pre}article order by id desc limit {$page->firstRow},1) order by l.id desc limit {$pageSize}";
		$data = $this->query($sql);
		
		$data['pageString'] = $pageString;
		
		return $data;
	}
}